package com.zff.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zff.dao.CgzpWorksDao;
import com.zff.dao.XstWxUserDao;
import com.zff.dao.XszpOrderDao;
import com.zff.entity.CgzpWorks;
import com.zff.entity.XstWxUser;
import com.zff.entity.XszpOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成果展评与论文展评的新增与修改
 * TODO
 * @version 1.0
 * @author Zff
 * @date 2021/11/2 14:57
 */
@RestController
@RequestMapping("/xszpOrder")
public class XszpOrderController {

    @Autowired
    private XszpOrderDao xszpOrderDao;

    @Autowired
    private XstWxUserDao xstWxUserDao;

    @Autowired
    private CgzpWorksDao cgzpWorksDao;



    /**
     * 根据userId获取订单列表
     * @param userId
     * @return
     */
    @GetMapping("/getOrderList")
    public Map getOrderList(Integer userId){
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<XszpOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userId).isNotNull("transactionid");
        List<XszpOrder> xszpOrders = xszpOrderDao.selectList(queryWrapper);
        if(xszpOrders.size()>0){
            map.put("data", xszpOrders);
            map.put("states", 1);
            map.put("msg", "获取成功");
        } else {
            map.put("states", 0);
            map.put("msg", "获取失败");
        }
        return map;
    }

    /**
     * 根据orderno获取订单详情中的用户
     * @param orderno
     * @return
     */
    @GetMapping("/getOrderInfo")
    public Map getOrderInfo(String orderno){
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<XszpOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderno",orderno).isNotNull("transactionid");
        XszpOrder xszpOrder = xszpOrderDao.selectOne(queryWrapper);
        Integer userid = xszpOrder.getUserid();
        XstWxUser xstWxUser = xstWxUserDao.selectById(userid);
        if(xstWxUser !=null){
            map.put("data", xstWxUser);
            map.put("xszpOrder", xszpOrder);
            map.put("states", 1);
            map.put("msg", "获取成功");
        } else {
            map.put("states", 0);
            map.put("msg", "获取失败");
        }
        return map;
    }


    /**
     * 根据oid，orderno修改添加作品信息
     * @param orderno
     * @return
     */
    @GetMapping("/updateOrderno")
    public Map updateOrderno(Integer oid,String orderno,String worksname,String worksPath,String sqspath){
        HashMap<String, Object> map = new HashMap<>();
        XszpOrder xszpOrder = new XszpOrder();
        xszpOrder.setOid(oid);
        xszpOrder.setOrderno(orderno);
        xszpOrder.setWorksname(worksname);
        xszpOrder.setWorkspath(worksPath);
        xszpOrder.setSqspath(sqspath);
        int count = xszpOrderDao.updateById(xszpOrder);
        if(count>0){
            map.put("states", 1);
            map.put("msg", "保存成功");
        }else {
            map.put("states", 0);
            map.put("msg", "保存失败");
        }
        return map;
    }

    /**
     * 新增成果展评
     * @param userid
     * @return
     */
    @GetMapping("/inscgzp")
    public Map inscgzp(Integer userid,String worksname,String worksPath,String cgsqtable){
        HashMap<String, Object> map = new HashMap<>();
        CgzpWorks cgzpWorks = new CgzpWorks();
        cgzpWorks.setUserid(userid);
        cgzpWorks.setWorksname(worksname);
        cgzpWorks.setWorkspath(worksPath);
        cgzpWorks.setCgsqtable(cgsqtable);
        cgzpWorks.setPrize("5");
        int count = cgzpWorksDao.insert(cgzpWorks);
        if(count>0){
            map.put("states", 1);
            map.put("msg", "保存成功");
        }else {
            map.put("states", 0);
            map.put("msg", "保存失败");
        }
        return map;
    }

    /**
     *修改成果展评
     * @return
     */
    @GetMapping("/updatecgzp")
    public Map updatecgzp(Integer wid,String worksname,String worksPath){
        HashMap<String, Object> map = new HashMap<>();
        CgzpWorks cgzpWorks = new CgzpWorks();
        cgzpWorks.setWid(wid);
        cgzpWorks.setWorksname(worksname);
        cgzpWorks.setWorkspath(worksPath);
        int count = cgzpWorksDao.updateById(cgzpWorks);
        if(count>0){
            map.put("states", 1);
            map.put("msg", "保存成功");
        }else {
            map.put("states", 0);
            map.put("msg", "保存失败");
        }
        return map;
    }

    /**
     * 根据userid获取成果展评列表
     * @param userid
     * @return
     */
    @GetMapping("/getCgzpList")
    public Map getCgzpList(Integer userid){
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<CgzpWorks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        List<CgzpWorks> cgzpWorks = cgzpWorksDao.selectList(queryWrapper);
        if(cgzpWorks.size()>0){
            map.put("data", cgzpWorks);
            map.put("states", 1);
            map.put("msg", "保存成功");
        }else {
            map.put("states", 0);
            map.put("msg", "保存失败");
        }
        return map;
    }

    /**
     * 根据wid获取成果展评详细
     * @param wid
     * @return
     */
    @GetMapping("/getCgzpInfo")
    public Map getCgzpInfo(Integer wid){
        HashMap<String, Object> map = new HashMap<>();
        CgzpWorks cgzpWorks = cgzpWorksDao.selectById(wid);
        if(cgzpWorks != null){
            map.put("data", cgzpWorks);
            map.put("states", 1);
            map.put("msg", "保存成功");
        }else {
            map.put("states", 0);
            map.put("msg", "保存失败");
        }
        return map;
    }



}
